/**
 * 
 */
package com.dsteiner.xelavos.client.model;

import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;

import com.dsteiner.widgets.client.PropertyChangeCollection;
import com.dsteiner.xelavos.client.domain.Rating;
import com.dsteiner.xelavos.client.domain.TrackComments;
import com.dsteiner.xelavos.client.domain.User;

public class AuthenticationModel {
	private PropertyChangeCollection propertyChangeSupport = new PropertyChangeCollection(
			this);

	private List<Rating> ratings = new ArrayList<Rating>();
	private List<TrackComments> trackComments = new ArrayList<TrackComments>();
	
	private User user;

	private String message;

	public void addPropertyChangeListener(String propertyName,
			PropertyChangeListener listener) {
		propertyChangeSupport.registerPropertyChangeListener(propertyName,
				listener);
	}
	public void addRating(Rating ratingObj) {
		List<Rating> newRatings = new ArrayList<Rating>();
		newRatings.addAll(this.ratings);
		newRatings.add(ratingObj);
		setRatings(newRatings);
	}
	
	public void addTrackComments(TrackComments trackComments) {
		List<TrackComments> newTrackComments = new ArrayList<TrackComments>();
		newTrackComments.addAll(this.trackComments);
		newTrackComments.add(trackComments);
		setTrackComments(newTrackComments);
	}
	public String getMessage() {
		return message;
	}

	public List<Rating> getRatings() {
		return ratings;
	}

	public List<TrackComments> getTrackComments() {
		return trackComments;
	}
	
	public User getUser() {
		return user;
	}

	public boolean isAlreadyRated(String rateObjId){
		for (Rating rating : ratings) {
			if(rating.getRefId().equals(rateObjId))
				return true;
		}
		return false;
	}

	public void removeRating(Rating ratingObj) {
		List<Rating> newRatings = new ArrayList<Rating>();
		newRatings.addAll(this.ratings);
		newRatings.remove(ratingObj);
		setRatings(newRatings);
	}

	public void removeTrackComments(TrackComments trackComments) {
		List<TrackComments> newTrackComments = new ArrayList<TrackComments>();
		newTrackComments.addAll(this.trackComments);
		newTrackComments.remove(trackComments);
		setTrackComments(newTrackComments);
	}
	
	public void setMessage(String message) {
		String oldMessage = this.message;
		this.message = message;
		propertyChangeSupport.firePropertyChange("message", oldMessage, message);
	}

	public void setRatings(List<Rating> ratings) {
		List<Rating> oldRatings = this.ratings;
		this.ratings = ratings;
		propertyChangeSupport.firePropertyChange("ratings", oldRatings, ratings);
	}
	
	public void setTrackComments(List<TrackComments> trackComments) {
		List<TrackComments> oldTrackComments = this.trackComments;
		this.trackComments = trackComments;
		propertyChangeSupport.firePropertyChange("trackComments", oldTrackComments, trackComments);
	}

	public void setUser(User user) {
		User oldUser = this.user;
		this.user = user;
		propertyChangeSupport.firePropertyChange("user", oldUser, user);
	}
	
	public TrackComments getTrackComment(String objectId) {
		for (TrackComments trackComments : this.trackComments) {
			if(trackComments.getObjectId().equals(objectId))
				return trackComments;
		}
		return null;
	}
}